package com.qqb.controller;


import com.github.pagehelper.Page;
import com.qqb.pojo.Officer;
import com.qqb.pojo.Result;
import com.qqb.service.OfficerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/officer")
public class OfficerController {

    @Autowired
    private OfficerService officerService;

    /**
     * 分页查询
     *
     * @param condition 查询条件
     * @param pageNum   当前页
     * @param pageSize  每页大小
     * @return
     */
    @PostMapping("/searchPageList/{pageNum}/{pageSize}")
    public Map<String, Object> searchPageList(@RequestBody(required = false) Map<String, String> condition,
                                              @PathVariable String pageNum,
                                              @PathVariable String pageSize) {

        if (pageNum == null || "".equals(pageNum)) {
            pageNum = "1";
        }
        if (pageSize == null || "".equals(pageSize)) {
            pageSize= "3";
        }
        Page<Officer> pageList = officerService.searchPageList(condition, pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pageDate", pageList);
        map.put("totalNum", pageList.getTotal());
        return map;
    }


    @PostMapping("/add")
    public Result add(@RequestBody Officer officer) {
        officerService.add(officer);
        return new Result();
    }


    @PostMapping("/update")
    public Result update(@RequestBody Officer officer) {
        officerService.update(officer);
        return new Result();
    }


    @PostMapping("/delete")
    public Result delete(@RequestBody String[] ids) {
        officerService.delete(ids);
        return new Result();
    }

}
